//
//  FastPaymentViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/8/17.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "FastPaymentViewController.h"
#import "FastPaymentCell.h"
#import "FastPaymentModel.h"
#import "RSAEncryptor.h"
#import "BankVeriftyViewController.h"

@interface FastPaymentViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, weak)UILabel * usedCardLabel;

@property (nonatomic, strong)UITableView * tableView;

@property (nonatomic, strong)NSArray<FastPaymentModel *> * bankCardList;

@property (nonatomic, weak)UILabel * addCardLabel;

//=========
@property (nonatomic, weak)UIView * backViewOne;

@property (nonatomic, weak)UILabel * telLabel;

@property (nonatomic, weak)UITextField * telTextField;

@property (nonatomic, weak)UIView * lineView;

@property (nonatomic, weak)UIView * backViewTwo;

@property (nonatomic, weak)UILabel * verifyLabel;

@property (nonatomic, weak)UILabel * verifyText;

@property (nonatomic, weak)UIView * lineViewTwo;

@property (nonatomic, weak)UIView * backViewThree;

@property (nonatomic, weak)UILabel * passwordLabel;

@property (nonatomic, weak)UITextField * passwordText;

@property (nonatomic, weak)UIView * lineViewThree;

@property (nonatomic, weak)UIView * backViewName;

@property (nonatomic, weak)UILabel * nameLabel;

@property (nonatomic, weak)UITextField * nameText;

@property (nonatomic, weak)UIView * lineViewName;

@property (nonatomic, weak)UIView * backViewFour;

@property (nonatomic, weak)UILabel * emailLabel;

@property (nonatomic, weak)UITextField * emailText;


@property (nonatomic, weak)UIButton * nextButton;

@end

@implementation FastPaymentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [FastPaymentModel fastModelListWithSucess:^(NSArray<FastPaymentModel *> *array) {
        
        self.bankCardList = array;
        
    } error:^{
        
    }];
    //注册观察键盘的变化
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(transformView:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    [self setupNavBar];
    [self setupUI];
}


- (void)setBankCardList:(NSArray<FastPaymentModel *> *)bankCardList{
    
    _bankCardList = bankCardList;
    
    [self.tableView reloadData];
}


- (void)setupNavBar{
    
    self.title = @"快捷支付";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    
    UILabel * usedCardLabel = [UILabel new];
    
    usedCardLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    usedCardLabel.textColor = ColorWithRGB(0xc8c8c8);
    usedCardLabel.text = @"使用过的银行卡";
    
    self.usedCardLabel = usedCardLabel;
    
    [self.view addSubview:self.usedCardLabel];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 110 * SCREENH6, SCREEN_WIDTH, 150 * SCREENH6)];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = NO;
    
    [self.tableView registerClass:[FastPaymentCell class] forCellReuseIdentifier:@"fastPayCell"];
    
    [self.view addSubview:self.tableView];
    
    
    UILabel * addCardLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * SCREENH6, 265 * SCREENH6, 200 * SCREENH6, 20 * SCREENH6)];
    
    addCardLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    addCardLabel.textColor = ColorWithRGB(0xc8c8c8);
    addCardLabel.text = @"使用新卡，请填写一下信息：";
    
    self.addCardLabel = addCardLabel;
    
    [self.view addSubview:self.addCardLabel];
    
    //===============
    UIView * backViewOne = [[UIView alloc] init];
    
    backViewOne.backgroundColor = [UIColor whiteColor];
    backViewOne.alpha = 0.1;
    
    self.backViewOne = backViewOne;
    
    [self.view addSubview:self.backViewOne];
    
    UILabel * telLabel = [[UILabel alloc] init];
    
    telLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    telLabel.textColor = [UIColor whiteColor];//ColorWithRGB(0xeeeeee);
    telLabel.text = @"卡号";
    
    self.telLabel = telLabel;
    
    [self.view addSubview:self.telLabel];
    
    UITextField * telText = [[UITextField alloc] init];
    
    telText.borderStyle = UITextBorderStyleNone;
    telText.backgroundColor = [UIColor clearColor];
    
    //设置光标颜色
    telText.tintColor = ColorWithRGB(0x808486);
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0x808486);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    telText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入银行卡号" attributes:attrs];
    telText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    telText.textColor = ColorWithRGB(0x808486);
    telText.keyboardType = UIKeyboardTypeNumberPad;
    telText.keyboardAppearance = UIKeyboardAppearanceDark;
    telText.delegate = self;
    
    self.telTextField = telText;
    
    [self.view addSubview:self.telTextField];
    
    
    UIView * lineView = [[UIView alloc] init];
    
    lineView.backgroundColor = ColorWithRGB(0x526366);
    
    self.lineView = lineView;
    
    [self.view addSubview:self.lineView];
    
    UIView * backViewTwo = [[UIView alloc] init];
    
    backViewTwo.backgroundColor = [UIColor whiteColor];
    backViewTwo.alpha = 0.1;
    
    self.backViewTwo = backViewTwo;
    
    [self.view addSubview:self.backViewTwo];
    
    UILabel * verifty = [[UILabel alloc] init];
    
    verifty.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    verifty.textColor = ColorWithRGB(0xeeeeee);
    verifty.textAlignment = NSTextAlignmentLeft;
    verifty.text = @"银行";
    
    self.verifyLabel = verifty;
    
    [self.view addSubview:self.verifyLabel];
    
    
    UILabel * veriftyText = [[UILabel alloc] init];
    
    //设置光标颜色
    veriftyText.textColor = ColorWithRGB(0x808486);
    veriftyText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    
    self.verifyText = veriftyText;
    
    [self.view addSubview:self.verifyText];
    
    UIView * lineViewTwo = [[UIView alloc] init];
    
    lineViewTwo.backgroundColor = ColorWithRGB(0x526366);
    
    self.lineViewTwo = lineViewTwo;
    
    [self.view addSubview:self.lineViewTwo];
    
    
    UIView * backViewThree = [[UIView alloc] init];
    
    backViewThree.backgroundColor = [UIColor whiteColor];
    backViewThree.alpha = 0.1;
    
    self.backViewThree = backViewThree;
    
    [self.view addSubview:self.backViewThree];
    
    UILabel * passwordLabel = [[UILabel alloc] init];
    
    passwordLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    passwordLabel.textColor = ColorWithRGB(0xeeeeee);
    passwordLabel.text = @"姓名";
    
    self.passwordLabel = passwordLabel;
    
    [self.view addSubview:self.passwordLabel];
    
    UITextField * passwordText = [[UITextField alloc] init];
    
    passwordText.borderStyle = UITextBorderStyleNone;
    passwordText.backgroundColor = [UIColor clearColor];
    
    //设置光标颜色
    passwordText.tintColor = ColorWithRGB(0x808486);
    
    passwordText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入持卡人姓名" attributes:attrs];
    
    passwordText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    passwordText.textColor = ColorWithRGB(0x808486);
    passwordText.keyboardType = UIKeyboardTypeDefault;
    passwordText.keyboardAppearance = UIKeyboardAppearanceDark;
    
    
    self.passwordText = passwordText;
    
    [self.view addSubview:self.passwordText];
    
    UIView * lineViewThree = [[UIView alloc] init];
    
    lineViewThree.backgroundColor = ColorWithRGB(0x526366);
    
    self.lineViewThree = lineViewThree;
    
    [self.view addSubview:self.lineViewThree];
    
    
    UIView * backViewName = [[UIView alloc] init];
    
    backViewName.backgroundColor = [UIColor whiteColor];
    backViewName.alpha = 0.1;
    
    self.backViewName = backViewName;
    
    [self.view addSubview:self.backViewName];
    
    
    UILabel * nameLabel = [[UILabel alloc] init];
    
    nameLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    nameLabel.textColor = ColorWithRGB(0xeeeeee);
    nameLabel.text = @"证件号";
    
    self.nameLabel = nameLabel;
    
    [self.view addSubview:self.nameLabel];
    
    
    UITextField * nameText = [[UITextField alloc] init];
    
    nameText.borderStyle = UITextBorderStyleNone;
    nameText.backgroundColor = [UIColor clearColor];
    
    //设置光标颜色
    nameText.tintColor = ColorWithRGB(0x808486);
    
    nameText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入身份证号" attributes:attrs];
    nameText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    nameText.textColor = ColorWithRGB(0x808486);
    nameText.keyboardType = UIKeyboardTypeDefault;
    nameText.keyboardAppearance = UIKeyboardAppearanceDark;
    
    
    self.nameText = nameText;
    
    [self.view addSubview:self.nameText];
    
    
    UIView * lineViewName = [[UIView alloc] init];
    
    lineViewName.backgroundColor = ColorWithRGB(0x526366);
    
    self.lineViewName = lineViewName;
    
    [self.view addSubview:self.lineViewName];
    
    
    
    UIView * backViewFour = [[UIView alloc] init];
    
    backViewFour.backgroundColor = [UIColor whiteColor];
    backViewFour.alpha = 0.1;
    
    self.backViewFour = backViewFour;
    
    [self.view addSubview:self.backViewFour];
    
    UILabel * emailLabel = [[UILabel alloc] init];
    
    emailLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    emailLabel.textColor = ColorWithRGB(0xeeeeee);
    emailLabel.text = @"电话";
    
    self.emailLabel = emailLabel;
    
    [self.view addSubview:self.emailLabel];
    
    
    UITextField * emailText = [[UITextField alloc] init];
    
    emailText.borderStyle = UITextBorderStyleNone;
    emailText.backgroundColor = [UIColor clearColor];
    
    //设置光标颜色
    emailText.tintColor = ColorWithRGB(0x808486);
    
    emailText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入银行预留手机号" attributes:attrs];
    emailText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    emailText.textColor = ColorWithRGB(0x808486);
    emailText.keyboardAppearance = UIKeyboardAppearanceDark;
    emailText.keyboardType = UIKeyboardTypeNumberPad;
    
    self.emailText = emailText;
    
    [self.view addSubview:self.emailText];
    
    
    UIButton * nextButton = [[UIButton alloc] init];
    
    [nextButton setTitle:@"支付" forState:UIControlStateNormal];
    nextButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17 * SCREENH6];//
    [nextButton setBackgroundImage:[UIImage imageNamed:@"注册"] forState:UIControlStateNormal];
    nextButton.layer.borderColor = [[UIColor blackColor] CGColor];
    //边框宽度
    nextButton.layer.borderWidth =1.0f;
    //处理圆角
    nextButton.layer.cornerRadius =5.0f;
    [nextButton setTitleColor:ColorWithRGB(0xffffff) forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(didPayButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.nextButton = nextButton;
    
    [self.view addSubview:self.nextButton];
}



- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
    [self.usedCardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(80 * SCREENH6);
    }];
 
    [self.backViewOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(345 * SCREENH6);
        make.height.mas_equalTo(54 * SCREENH6);
        make.top.mas_equalTo(self.addCardLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.telLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backViewOne).offset(9 * SCREENH6);
        make.centerY.mas_equalTo(self.backViewOne);
        make.width.mas_equalTo(45 * SCREENH6);
    }];
    
    [self.telTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backViewOne);
        make.leading.mas_equalTo(self.telLabel.mas_trailing).offset(42 * SCREENH6);
        make.height.mas_equalTo(self.telLabel);
        make.width.mas_equalTo(250 * SCREENH6);
    }];
    
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1 * SCREENH6);
        //        make.width.mas_equalTo(323 * SCREENH6);
        make.leading.trailing.mas_equalTo(self.backViewOne);
        make.top.mas_equalTo(self.backViewOne.mas_bottom);
    }];
    
    [self.backViewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lineView.mas_bottom);
        make.height.width.leading.trailing.mas_equalTo(self.backViewOne);
    }];
    
    [self.verifyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.width.height.mas_equalTo(self.telLabel);
        make.centerY.mas_equalTo(self.backViewTwo);
    }];
    
    [self.verifyText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.width.height.mas_equalTo(self.telTextField);
        make.centerY.mas_equalTo(self.backViewTwo);
    }];
    
    [self.lineViewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1 * SCREENH6);
        //        make.width.mas_equalTo(323 * SCREENH6);
        make.leading.trailing.mas_equalTo(self.backViewTwo);
        make.top.mas_equalTo(self.backViewTwo.mas_bottom);
    }];
    
    [self.backViewThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.width.height.mas_equalTo(self.backViewTwo);
        make.top.mas_equalTo(self.lineViewTwo.mas_bottom);
    }];
    
    [self.passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.width.height.mas_equalTo(self.verifyLabel);
        make.centerY.mas_equalTo(self.backViewThree);
    }];
    
    [self.passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.width.mas_equalTo(self.verifyText);
        make.centerY.mas_equalTo(self.backViewThree);
//        make.trailing.mas_equalTo(self.sentMesButton);
    }];
    
    [self.lineViewThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1 * SCREENH6);
        //        make.width.mas_equalTo(323 * SCREENH6);
        make.leading.trailing.mas_equalTo(self.backViewTwo);
        make.top.mas_equalTo(self.backViewThree.mas_bottom);
    }];
    
    [self.backViewName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.width.height.mas_equalTo(self.backViewThree);
        make.top.mas_equalTo(self.lineViewThree.mas_bottom);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.width.height.mas_equalTo(self.passwordLabel);
        make.centerY.mas_equalTo(self.backViewName);
    }];
    
    [self.nameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.mas_equalTo(self.passwordText);
        make.width.mas_equalTo(200 * SCREENH6);
        make.centerY.mas_equalTo(self.backViewName);
    }];
    
    [self.lineViewName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1 * SCREENH6);
        //        make.width.mas_equalTo(323 * SCREENH6);
        make.leading.trailing.mas_equalTo(self.backViewName);
        make.top.mas_equalTo(self.backViewName.mas_bottom);
    }];
    
    
    [self.backViewFour mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.width.height.mas_equalTo(self.backViewName);
        make.top.mas_equalTo(self.lineViewName.mas_bottom);
    }];
    
    [self.emailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.width.height.mas_equalTo(self.passwordLabel);
        make.centerY.mas_equalTo(self.backViewFour);
    }];
    
    [self.emailText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.width.height.mas_equalTo(self.nameText);
        make.centerY.mas_equalTo(self.backViewFour);
    }];
    
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backViewFour.mas_bottom).offset(20 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.height.mas_equalTo(46 * SCREENH6);
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.bankCardList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FastPaymentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fastPayCell" forIndexPath:indexPath];
    
    //model
    FastPaymentModel *model = self.bankCardList[indexPath.row];
    
    cell.model = model;
    
    return cell;
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    FastPaymentCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSDictionary * parameters = @{@"amount":@"500",@"bankId":cell.ID};
    
    [[NetworkTool sharedTools] POST:@"pay/yl" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        
        NSDictionary * paramDict = responseObject[@"param"];
        
        if ([code isEqualToString:@"1000"]) {
            
            self.hidesBottomBarWhenPushed = YES;
            
            BankVeriftyViewController * bankVeriftyVC = [BankVeriftyViewController new];
            
            bankVeriftyVC.payNo = [NSString stringWithFormat:@"%@",paramDict[@"jobId"]];
            bankVeriftyVC.orderNo = [NSString stringWithFormat:@"%@",paramDict[@"orderNo"]];
            bankVeriftyVC.orderCode = [NSString stringWithFormat:@"%@",paramDict[@"orderId"]];
            
            [self.navigationController pushViewController:bankVeriftyVC animated:YES];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }else if ([code isEqualToString:@"0"]){
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"恭喜你，支付成功！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"loadUserInfo" object:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"服务器内部错误，请稍后再试！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}


//点击支付按钮
- (void)didPayButton{
    
    [self.telTextField resignFirstResponder];
    [self.passwordText resignFirstResponder];
    [self.emailText resignFirstResponder];
    [self.nameText resignFirstResponder];
    
    if ([self.telTextField.text isEqualToString:@""] || [self.passwordText.text isEqualToString:@""] || [self.emailText.text isEqualToString:@""] || [self.nameText.text isEqualToString:@""]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请完善银行卡信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        //pay/yl
        NSString *public_key_path = [[NSBundle mainBundle] pathForResource:@"public_key.der" ofType:nil];
        
        NSString * bankNo = [RSAEncryptor encryptString:self.telTextField.text publicKeyWithContentsOfFile:public_key_path];
        NSString * phone = [RSAEncryptor encryptString:self.emailText.text publicKeyWithContentsOfFile:public_key_path];
        NSString * fullName = [RSAEncryptor encryptString:self.passwordText.text publicKeyWithContentsOfFile:public_key_path];
        NSString * cardNo = [RSAEncryptor encryptString:self.nameText.text publicKeyWithContentsOfFile:public_key_path];
        
        NSDictionary * parameters = @{@"amount":@"500",@"bankNo":bankNo,@"phone":phone,@"fullName":fullName,@"cardNo":cardNo};
        
        [[NetworkTool sharedTools] POST:@"pay/yl" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            NSLog(@"==%@",responseObject);
            
            NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
            
            
            NSDictionary * paramDict = responseObject[@"param"];
            
            if ([code isEqualToString:@"1000"]) {
                
                self.hidesBottomBarWhenPushed = YES;
                
                BankVeriftyViewController * bankVeriftyVC = [BankVeriftyViewController new];
                
                bankVeriftyVC.payNo = [NSString stringWithFormat:@"%@",paramDict[@"jobId"]];
                bankVeriftyVC.orderNo = [NSString stringWithFormat:@"%@",paramDict[@"orderNo"]];
                bankVeriftyVC.orderCode = [NSString stringWithFormat:@"%@",paramDict[@"orderId"]];
                int historyId = [paramDict[@"historyId"] intValue];
                
                NSString * historyIdStr;
                if (historyId > 0) {                    
                    historyIdStr = [NSString stringWithFormat:@"%d",historyId];
                }else{
                    historyIdStr = @"";
                    
                }
                 bankVeriftyVC.historyId = historyIdStr;
                
                [self.navigationController pushViewController:bankVeriftyVC animated:YES];
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            }else if ([code isEqualToString:@"0"]){
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"恭喜你，支付成功！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                          [self.navigationController popViewControllerAnimated:YES];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"loadUserInfo" object:nil];
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"服务器内部错误，请稍后再试！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
    
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    
//    self.telTextField.text;
    NSDictionary * parameters = @{@"bankCard":textField.text};
    
    [[NetworkTool sharedTools] POST:@"site/bank-card-info" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        
        if ([code isEqualToString:@"0"]) {
            NSDictionary * param = responseObject[@"param"];
            
            self.verifyText.text = [NSString stringWithFormat:@"%@",param[@"bank"]];
        }else{
           
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.telTextField resignFirstResponder];
    [self.passwordText resignFirstResponder];
    [self.emailText resignFirstResponder];
    [self.nameText resignFirstResponder];
}


//键盘回收
//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    for(UIView *view in self.view.subviews)
//    {
//        [view resignFirstResponder];
//    }
//}

//移动UIView
-(void)transformView:(NSNotification *)aNSNotification
{
    //获取键盘弹出前的Rect
    NSValue *keyBoardBeginBounds=[[aNSNotification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect beginRect=[keyBoardBeginBounds CGRectValue];
    
    //获取键盘弹出后的Rect
    NSValue *keyBoardEndBounds=[[aNSNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect  endRect=[keyBoardEndBounds CGRectValue];
    
    //获取键盘位置变化前后纵坐标Y的变化值
    CGFloat deltaY=endRect.origin.y-beginRect.origin.y;
//    NSLog(@"看看这个变化的Y值:%f",deltaY);
    
    //在0.25s内完成self.view的Frame的变化，等于是给self.view添加一个向上移动deltaY的动画
    [UIView animateWithDuration:0.25f animations:^{
        [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+deltaY, self.view.frame.size.width, self.view.frame.size.height)];
    }];
}


- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
